package com.ionehe.group.common.notify.eventbus.producer;

import com.google.common.eventbus.AsyncEventBus;
import com.google.common.eventbus.EventBus;
import com.ionehe.group.common.notify.event.listener.EventListener;
import com.ionehe.group.common.notify.event.producer.EventProducer;

import java.util.concurrent.Executors;

/**
 * Copyright (c) 2021 ionehe.com
 * Date: 2021/4/21
 * Time: 1:22 下午
 *
 * @author 2021年 <a href="mailto:a@ionehe.com">秀</a>
 * 【事件生产者行为】
 */
public class EventProducerImpl implements EventProducer {
    private final EventBus eventBus;

    public EventProducerImpl(String identifier) {
        int threadCount = Runtime.getRuntime().availableProcessors() + 1;
        this.eventBus = (EventBus) new AsyncEventBus(identifier, Executors.newFixedThreadPool(threadCount));
    }

    @Override
    public void send(Object event) {
        this.eventBus.post(event);
    }

    @Override
    public void addSubscriber(EventListener eventListener) {
        this.eventBus.register(eventListener);
    }
}


